package net.kindroid.fm;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * record digit time display view
 * 
 * @author huasong.chai
 */
public class TimeDigitView extends LinearLayout
{
	private ImageView mImage_hour_high;
	private ImageView mImage_hour_low;
	private ImageView mImage_hour_colon;

	private ImageView mImage_minute_high;
	private ImageView mImage_minute_low;
	private ImageView mImage_minute_colon;

	private ImageView mImage_second_high;
	private ImageView mImage_second_low;

	public TimeDigitView(Context context, AttributeSet attrs)
	{
		super(context, attrs);

		mImage_hour_high = new ImageView(context, attrs);
		mImage_hour_low = new ImageView(context, attrs);
		mImage_hour_colon = new ImageView(context, attrs);
		mImage_minute_high = new ImageView(context, attrs);
		mImage_minute_low = new ImageView(context, attrs);
		mImage_minute_colon = new ImageView(context, attrs);
		mImage_second_high = new ImageView(context, attrs);
		mImage_second_low = new ImageView(context, attrs);

		setClickable(false);
		setFocusable(true);
		setOrientation(LinearLayout.HORIZONTAL);

		mImage_hour_high.setBackgroundResource(setTimeDigitResource(0));
		mImage_hour_low.setBackgroundResource(setTimeDigitResource(0));
		mImage_hour_colon.setBackgroundResource(setTimeDigitResource(-1));
		mImage_minute_high.setBackgroundResource(setTimeDigitResource(0));
		mImage_minute_low.setBackgroundResource(setTimeDigitResource(0));
		mImage_minute_colon.setBackgroundResource(setTimeDigitResource(-1));
		mImage_second_high.setBackgroundResource(setTimeDigitResource(0));
		mImage_second_low.setBackgroundResource(setTimeDigitResource(0));

		addView(mImage_hour_high);
		addView(mImage_hour_low);
		addView(mImage_hour_colon);
		addView(mImage_minute_high);
		addView(mImage_minute_low);
		addView(mImage_minute_colon);
		addView(mImage_second_high);
		addView(mImage_second_low);
	}

	/**
	 * set hour
	 * 
	 * @param hour
	 */
	public void setHour(int hour)
	{
		if (hour < 0)
		{
			hour = 0;
		}
		mImage_hour_high.setBackgroundResource(setTimeDigitResource(hour / 10));
		mImage_hour_low.setBackgroundResource(setTimeDigitResource(hour % 10));
	}

	/**
	 * set minute
	 * 
	 * @param minute
	 */
	public void setMinute(int minute)
	{
		if (minute > 60 || minute < 0)
		{
			minute = 0;
		}
		mImage_minute_high.setBackgroundResource(setTimeDigitResource(minute / 10));
		mImage_minute_low.setBackgroundResource(setTimeDigitResource(minute % 10));
	}

	/**
	 * set second
	 * 
	 * @param second
	 */
	public void setSecond(int second)
	{
		if (second > 60 || second < 0)
		{
			second = 0;
		}
		mImage_second_high.setBackgroundResource(setTimeDigitResource(second / 10));
		mImage_second_low.setBackgroundResource(setTimeDigitResource(second % 10));
	}

	private int setTimeDigitResource(int digit)
	{
		int resource = R.drawable.time_zero;
		switch (digit)
		{
			case -1:
				resource = R.drawable.time_colon;
				break;
			case 0:
				resource = R.drawable.time_zero;
				break;
			case 1:
				resource = R.drawable.time_one;
				break;
			case 2:
				resource = R.drawable.time_two;
				break;
			case 3:
				resource = R.drawable.time_three;
				break;
			case 4:
				resource = R.drawable.time_four;
				break;
			case 5:
				resource = R.drawable.time_five;
				break;
			case 6:
				resource = R.drawable.time_six;
				break;
			case 7:
				resource = R.drawable.time_seven;
				break;
			case 8:
				resource = R.drawable.time_eight;
				break;
			case 9:
				resource = R.drawable.time_nine;
				break;
			case 10:
				resource = R.drawable.transparent;
				break;
		}
		return resource;
	}

}
